﻿Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports DevExpress.XtraBars
Imports System.Data
Imports System.Linq
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Linq.Expressions
Imports System.Reflection

Imports Publics

Public Class FrmChonDonVi

#Region "UCProperty"
    Private _ObjDonVi As DM_DonVi
    Private _vDataSource As Object

    Public Property ObjDonVi() As DM_DonVi
        Get
            Return _ObjDonVi
        End Get
        Set(ByVal value As DM_DonVi)
            _ObjDonVi = value
        End Set
    End Property

    Public Property vDataSource() As Object
        Get
            Return _vDataSource
        End Get
        Set(ByVal value As Object)
            _vDataSource = value
        End Set
    End Property

#End Region

    Private Sub iButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iButtonCancel.Click
        Me.Close()
    End Sub

    Private Sub iButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iButtonOk.Click
        Dim iDepartment As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                     Where ds.ID.Equals(CStr(Me.vTreeListDonVi.FocusedNode.GetValue(Me.iColumnID))) _
                                     Select ds).FirstOrDefault()
        Me.ObjDonVi = iDepartment
        Me.Close()
    End Sub

    ''' <summary>
    ''' Hiển thị thông tin phím tắt trên caption của form
    ''' @NhiLV
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetCaptionForm()
        Dim strChon As String = String.Empty
        Dim strThoat As String = GetStrKey(kThoat)
        If iButtonOk.Visible = True And iButtonOk.Enabled = True Then
            strChon = GetStrKey(kChon)
        End If

        'If btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnTroGiup.Enabled = True Then
        '    strTroGiup = GetStrKey(kTroGiup)
        'End If
        Me.Text = Me.Text + strChon + strThoat 'strTroGiup + 
    End Sub

    Private Sub iDepartmentDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = "Chọn đơn vị"
        If Me.vDataSource IsNot Nothing Then
            Me.vTreeListDonVi.DataSource = Me.vDataSource
        End If
        If ObjDonVi IsNot Nothing Then
            Me.vTreeListDonVi.FocusedNode = Me.vTreeListDonVi.FindNodeByFieldValue(Me.iColumnID.FieldName, Me.ObjDonVi.ID)
        End If
        '/GetCaptionForm()
    End Sub

    Private Sub vTreeListDonVi_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles vTreeListDonVi.KeyDown
        If Me.vTreeListDonVi.FocusedNode IsNot Nothing Then
            Select Case e.KeyCode
                Case Keys.Right
                    Me.vTreeListDonVi.FocusedNode.Expanded = True
                Case Keys.Left
                    Me.vTreeListDonVi.FocusedNode.Expanded = False
                Case Keys.Enter
                    iButtonOk_Click(Nothing, Nothing)
            End Select
        End If
    End Sub

    Private Sub iDepartmentDialog_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        'If e.KeyChar = ChrW(Keys.Escape) Then
        '    Me.Close()
        'End If
    End Sub

    Private Sub FrmChonDonVi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Chấp nhận
        If (e.Modifiers = GetKeyModifide(pIdChon) And _
                                e.KeyCode = GetKeyCode2(pIdChon)) Or _
                                (e.KeyCode = GetKeyCode(pIdChon)) Then

            If Me.iButtonOk.Enabled = True And Me.iButtonOk.Visible = True Then
                iButtonOk_Click(Nothing, Nothing)
            End If
        End If
        'Huỷ bỏ (thoát)
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
    e.KeyCode = GetKeyCode2(pIdThoat)) Or _
    (e.KeyCode = GetKeyCode(pIdThoat)) Then
            iButtonCancel_Click(Nothing, Nothing)
            'Me.Close()
        End If
    End Sub
End Class